var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var MySQLStore = require('express-mysql-session')(session);
var config=require('./dbhelper');

var index = require('./routes/index');
var users = require('./routes/users');
var menu = require('./routes/menu');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'ejs');
app.engine('html', require('ejs').__express); // 使用ejs引擎解析html文件中ejs语法
app.set('view engine', 'html');

// uncomment after placing your favicon in /public
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

process.on('uncaughtException', function (err) {
  console.error(err);
}) //监听未捕获的异常


//app.set('trust proxy', 1) // trust first proxy
app.disable('x-powered-by');

/* var options = {
  host: 'localhost',
  port: 3306,
  user: 'session_test',
  password: 'password',
  database: 'session_test'
}; */

var options=config.config.dbconnection.mysql.connection;

var sessionStore = new MySQLStore(options);

// 使用 session 中间件
app.use(session({
  secret: 'qwerwsadasdsawq12312312wqdsdasdwqfrwqrqszfd', // 对session id 相关的cookie 进行签名
  store: sessionStore,
  resave: false,
  saveUninitialized: false, // 是否保存未初始化的会话
  cookie: {
    maxAge: 1000 * 60 * 60 * 6, // 设置 session 的有效时间，单位毫秒  6小时
    //secure: true
  },
}));





app.use('/login', require('./routes/login'));

app.use(function (req, res, next) {
  if (req.session.user === undefined) {
    res.render('login');
  } else {
    next();
  }
});

app.use('/', index);
app.use('/users', users);
app.use('/menu', menu);
app.use('/fdm', require('./routes/fdm'));
app.use('/task', require('./routes/task'));
app.use('/dataset', require('./routes/dataset'));
app.use('/workflow', require('./routes/workflow'));
app.use('/statistics', require('./routes/statistics'));



// catch 404 and forward to error handler
app.use(function (req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  console.error(err);
  if (err.message.indexOf('Failed to lookup view') > -1) {
    err.status = 404;
  }
  // render the error page
  res.status(err.status || 500);
  if (err.status == 404) {
    res.render('404');
  } else {
    res.render('500');
  }
});
/* var arr = [ 1, 2, 3, 4, 5, 6 ]; 
arr.every(function (item, index, array) {
  console.log('item=' + item + ',index=' + index + ',array=' + array);
  return index <arr.length;
}); */


//var services = require('./services/fileimport');
/* services.import(); */
/* services.filedisplay(); */

module.exports = app;